//
//  ConsultViewController.m
//  MedicineBox
//
//  Created by dllo on 15/9/29.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "ConsultViewController.h"
#import "ImageCollectionViewCell.h"
#import "AsyncConnection.h"
#import "ConsultModel.h"
#import "ConsultWebViewController.h"
#import "MJRefresh.h"

#define ScrollWidth self.view.frame.size.width / 4
#define widths self.view.frame.size.width
@interface ConsultViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate, ImageCollectionViewCellDelegate>
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) UIScrollView *scroll;
@property (nonatomic, retain) NSMutableArray *firstArr;
@property (nonatomic, retain) NSMutableArray *secondArr;
@property (nonatomic, retain) NSMutableArray *thirdArr;
@property (nonatomic, retain) NSMutableArray *fourArr;
@property (nonatomic, retain) NSMutableArray *fiveArr;
@property (nonatomic, retain) NSMutableArray *sixArr;
@property (nonatomic, retain) NSMutableArray *bigArr;
@property (nonatomic, assign) NSInteger num;
@property (nonatomic, assign) NSInteger number;
@property (nonatomic, assign) NSInteger p1; // 上拉加载的一个参数
@property (nonatomic, assign) NSInteger p2;
@property (nonatomic, assign) NSInteger p3;
@property (nonatomic, assign) NSInteger p4;
@property (nonatomic, assign) NSInteger p5;
@property (nonatomic, assign) NSInteger p6;
@end

@implementation ConsultViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.firstArr = [NSMutableArray array];
        self.secondArr = [NSMutableArray array];
        self.thirdArr = [NSMutableArray array];
        self.fourArr = [NSMutableArray array];
        self.fiveArr = [NSMutableArray array];
        self.sixArr = [NSMutableArray array];
        self.bigArr = [NSMutableArray array];
        self.num = 1;
        self.number = 0;
        self.p1 = 1;
        self.p2 = 1;
        self.p3 = 1;
        self.p4 = 1;
        self.p5 = 1;
        self.p6 = 1;
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:86/255.0 green:137/255.0 blue:156/255.0 alpha:1];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 60, 0, 60, 33)];
    titleLabel.text = @"资讯";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont fontWithName:@"Baskerville-SemiBold" size:20];
    self.navigationItem.titleView = titleLabel;
    [titleLabel release];
//    self.navigationItem.title = @"资讯";
    [self beginConnection];
    [self creatCollectionView];
    
}



- (void)creatCollectionView
{
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
//    _scroll.backgroundColor = [UIColor redColor];
    _scroll.showsVerticalScrollIndicator = NO;
    _scroll.contentSize = CGSizeMake(ScrollWidth * 6, 0);
    [self.view addSubview:_scroll];
    // 循环创建button
    for (int i = 0; i < 6; i ++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(i * ScrollWidth, 0, ScrollWidth, 30);
//        button.backgroundColor = [UIColor yellowColor];
        [button setTag:i + 1];
        if (button.tag == 1) {
            [button setTitle:@"就医" forState: UIControlStateNormal];
            [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        }
        else if (button.tag == 2) {
            [button setTitle:@"热点" forState: UIControlStateNormal];
        }
        else if (button.tag == 3) {
            [button setTitle:@"母婴" forState: UIControlStateNormal];
        }
        else if (button.tag == 4) {
            [button setTitle:@"育儿" forState: UIControlStateNormal];
        }
        else if (button.tag == 5) {
            [button setTitle:@"两性" forState: UIControlStateNormal];
        }
        else if (button.tag == 6) {
            [button setTitle:@"养生" forState: UIControlStateNormal];
        }
        if (button.tag != 1) {
            [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        }
        [button addTarget:self action:@selector(firstAction:) forControlEvents:UIControlEventTouchUpInside];
        [_scroll addSubview:button];
        [button release];
    }
    
    
    // 下边的CollectionView套TableView
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
      self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake( 0, 30, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.bounces = NO;
    _collectionView.pagingEnabled = YES;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[ImageCollectionViewCell class] forCellWithReuseIdentifier:@"layout"];
    [self.view addSubview:_collectionView];
}

#pragma mark -获取scrollerView子视图的button 根据collection的偏移量控制scroller的偏移量
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger number = _collectionView.contentOffset.x / widths + 1;
    NSInteger num = _collectionView.contentOffset.x / widths;
    NSArray *arr = [self.scroll  subviews];
    for (UIButton *button in arr) {
        if ([button isKindOfClass:[UIButton class]]) {
            [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        }
        if (button.tag == number)
        [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }
    if (number > 4) {
        _scroll.contentOffset = CGPointMake(ScrollWidth * (number - 4), 0);
    }
    else if(num < 4){
    
        _scroll.contentOffset = CGPointMake(0, 0);
    }
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"layout" forIndexPath:indexPath];
    cell.tableArr = [_bigArr objectAtIndex:indexPath.item];
    cell.delegate = self;
    cell.index1 = indexPath;
//    NSLog(@"333333333================%@  %@" , indexPath , cell.index1) ;
    [cell.tableView reloadData];
    
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (_firstArr.count == 0) {
        return 0;
    }
    return _bigArr.count;
}

- (void)sendWebText:(NSString *)text
{
    ConsultWebViewController * webController = [[ConsultWebViewController alloc] init];
    webController.target = text;
    [self.navigationController pushViewController:webController animated:NO];
    [webController release];
}

//  下拉刷新
- (void)sendCollectionViewCell:(ImageCollectionViewCell *)cell
{
    NSLog(@"下拉刷新");
    self.number = 0;
    self.p1 = 1;
    self.p2 = 1;
    self.p3 = 1;
    self.p4 = 1;
    self.p5 = 1;
    self.p6 = 1;
    [self beginConnection];
    [cell.tableView.header endRefreshing];
    
}
// 上拉加载
- (void)sendCollectionIndex:(NSInteger)number
{
    if (number == 0) {
        _p1 ++;
        NSString *str = [NSString stringWithFormat:@"http://www.beiduo.cc/api.php/User/ihealth?p=%d&v_type=1", _p1];
        [AsyncConnection asyncConnectionWithUrl:str name:@"1" parameters:nil block:^(id data) {
            NSArray *dataArr = [data objectForKey:@"data"];
            for (NSDictionary *dic in dataArr) {
                
                ConsultModel *model = [[ConsultModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [_firstArr addObject:model];
                
            }
            [self reloadDataCollection];
        
        } get:@"get"];

    }
    if (number == 1) {
        _p2 ++;
        NSString *str = [NSString stringWithFormat:@"http://www.beiduo.cc/api.php/User/ihealth?p=%d&v_type=7", _p2];
        [AsyncConnection asyncConnectionWithUrl:str name:@"2" parameters:nil block:^(id data) {
            NSArray *dataArr = [data objectForKey:@"data"];
            for (NSDictionary *dic in dataArr) {
                
                ConsultModel *model = [[ConsultModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [_secondArr addObject:model];
                
            }
            [self reloadDataCollection];
            
        } get:@"get"];
        
    }
    if (number == 2) {
        _p3 ++;
        NSString *str = [NSString stringWithFormat:@"http://www.beiduo.cc/api.php/User/ihealth?p=%d&v_type=2", _p3];
        [AsyncConnection asyncConnectionWithUrl:str name:@"3" parameters:nil block:^(id data) {
            NSArray *dataArr = [data objectForKey:@"data"];
            for (NSDictionary *dic in dataArr) {
                
                ConsultModel *model = [[ConsultModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [_thirdArr addObject:model];
                
            }
            [self reloadDataCollection];
            
        } get:@"get"];
        
    }
    if (number == 3) {
        _p4 ++;
        NSString *str = [NSString stringWithFormat:@"http://www.beiduo.cc/api.php/User/ihealth?p=%d&v_type=3", _p4];
        [AsyncConnection asyncConnectionWithUrl:str name:@"4" parameters:nil block:^(id data) {
            NSArray *dataArr = [data objectForKey:@"data"];
            for (NSDictionary *dic in dataArr) {
                
                ConsultModel *model = [[ConsultModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [_fourArr addObject:model];
                
            }
            [self reloadDataCollection];
            
        } get:@"get"];
        
    }
    if (number == 4) {
        _p5 ++;
        NSString *str = [NSString stringWithFormat:@"http://www.beiduo.cc/api.php/User/ihealth?p=%d&v_type=4", _p5];
        [AsyncConnection asyncConnectionWithUrl:str name:@"5" parameters:nil block:^(id data) {
            NSArray *dataArr = [data objectForKey:@"data"];
            for (NSDictionary *dic in dataArr) {
                
                ConsultModel *model = [[ConsultModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [_fiveArr addObject:model];
                
            }
            [self reloadDataCollection];
            
        } get:@"get"];
        
    }
    if (number == 5) {
        _p6 ++;
        NSString *str = [NSString stringWithFormat:@"http://www.beiduo.cc/api.php/User/ihealth?p=%d&v_type=6", _p6];
        [AsyncConnection asyncConnectionWithUrl:str name:@"6" parameters:nil block:^(id data) {
            NSArray *dataArr = [data objectForKey:@"data"];
            for (NSDictionary *dic in dataArr) {
                
                ConsultModel *model = [[ConsultModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [_sixArr addObject:model];
                
            }
            [self reloadDataCollection];
            
        } get:@"get"];
        
    }




}


- (void)firstAction:(UIButton *)button
{
    
    // 获得父视图的button
    NSArray *arr = [[button superview] subviews];
    for (UIButton *bu in arr) {
        if ([bu isKindOfClass:[UIButton class]]) {
            [bu setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        }
    }
    // 点击其他按钮时被点击的按钮颜色发生改变
    if (button.tag != _num) {
        [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        _num = button.tag;
    }
    // 重复点击同一个button是 button颜色不发生改变
    if (button.tag == _num) {
        [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }
    
    if (button.tag == 1) {
        _collectionView.contentOffset = CGPointMake(0, 0);
    }
    else if (button.tag == 2){
        _collectionView.contentOffset = CGPointMake(widths, 0);
    }
    else if (button.tag == 3){
        _collectionView.contentOffset = CGPointMake(widths * 2, 0);
    }
    else if (button.tag == 4){
        _collectionView.contentOffset = CGPointMake(widths * 3, 0);
    }
    else if (button.tag == 5){
        _collectionView.contentOffset = CGPointMake(widths * 4, 0);
    }
    else if (button.tag == 6){
        _collectionView.contentOffset = CGPointMake(widths * 5, 0);
    }

    
}

- (void)beginConnection
{
    
    
    [AsyncConnection asyncConnectionWithUrl:@"http://www.beiduo.cc/api.php/User/ihealth?p=1&v_type=1" name:@"1" parameters:nil block:^(id data) {
        [_firstArr removeAllObjects];
        NSArray *dataArr = [data objectForKey:@"data"];
        for (NSDictionary *dic in dataArr) {
            
            ConsultModel *model = [[ConsultModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_firstArr addObject:model];
            
        }
        
        _number++;
        if (_number == 6) {
            [self reloadDataCollection];
        }
    } get:@"get"];
    [AsyncConnection asyncConnectionWithUrl:@"http://www.beiduo.cc/api.php/User/ihealth?p=1&v_type=7" name:@"2" parameters:nil block:^(id data) {
        [_secondArr removeAllObjects];
        NSArray *dataArr = [data objectForKey:@"data"];
        for (NSDictionary *dic in dataArr) {
            
            ConsultModel *model = [[ConsultModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_secondArr addObject:model];
            
        }
        
        _number++;
        if (_number == 6) {
            [self reloadDataCollection];
        }
        
    } get:@"get"];
    [AsyncConnection asyncConnectionWithUrl:@"http://www.beiduo.cc/api.php/User/ihealth?p=1&v_type=2" name:@"3" parameters:nil block:^(id data) {
        [_thirdArr removeAllObjects];
        NSArray *dataArr = [data objectForKey:@"data"];
        for (NSDictionary *dic in dataArr) {
            
            ConsultModel *model = [[ConsultModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_thirdArr addObject:model];
            
        }
        
        _number++;
        if (_number == 6) {
            [self reloadDataCollection];
        }
    } get:@"get"];
    [AsyncConnection asyncConnectionWithUrl:@"http://www.beiduo.cc/api.php/User/ihealth?p=1&v_type=3" name:@"4" parameters:nil block:^(id data) {
        [_fourArr removeAllObjects];
        NSArray *dataArr = [data objectForKey:@"data"];
        for (NSDictionary *dic in dataArr) {
            
            ConsultModel *model = [[ConsultModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_fourArr addObject:model];
            
        }
        
        _number++;
        if (_number == 6) {
            [self reloadDataCollection];
        }
    } get:@"get"];
    [AsyncConnection asyncConnectionWithUrl:@"http://www.beiduo.cc/api.php/User/ihealth?p=1&v_type=4" name:@"5" parameters:nil block:^(id data) {
        [_fiveArr removeAllObjects];
        NSArray *dataArr = [data objectForKey:@"data"];
        for (NSDictionary *dic in dataArr) {
            
            ConsultModel *model = [[ConsultModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_fiveArr addObject:model];
            
        }
        
        _number++;
        if (_number == 6) {
            [self reloadDataCollection];
        }
    } get:@"get"];
    [AsyncConnection asyncConnectionWithUrl:@"http://www.beiduo.cc/api.php/User/ihealth?p=1&v_type=6" name:@"6" parameters:nil block:^(id data) {
        [_sixArr removeAllObjects];
        NSArray *dataArr = [data objectForKey:@"data"];
        for (NSDictionary *dic in dataArr) {
            
            ConsultModel *model = [[ConsultModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_sixArr addObject:model];
            
        }
        
        _number++;
        if (_number == 6) {
            [self reloadDataCollection];
        }
    } get:@"get"];
   
    
}
// 第一次进入界面加载 和 下拉刷新时候调用
- (void)reloadDataCollection
{
    [_bigArr removeAllObjects];
    [_bigArr addObject:_firstArr];
    [_bigArr addObject:_secondArr];
    [_bigArr addObject:_thirdArr];
    [_bigArr addObject:_fourArr];
    [_bigArr addObject:_fiveArr];
    [_bigArr addObject:_sixArr];
    [_collectionView reloadData];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
